﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;

namespace MS.InformationManagement.Presenter.Pager
{
    public class PagerPresenter : BasePresenter<IPageView>
    {
        public PagerPresenter(IPageView view) : base(view) { }

        public void RebuildPager()
        {
            List<int> pages = new List<int>();
            if (View.TotalRecords > 0)
            {
                int pageCount = (View.TotalRecords - 1) / View.PageSize;

                int currentIndexCount = 0;
                int startingNumber = (View.PageIndex / View.NumberOfPagesToDisplay) * View.PageSize;

                if (startingNumber > 0)
                {
                    View.DisplayPreviousLink = true;
                    View.PreviousPageIndex = startingNumber - 1;
                }
                else { View.DisplayPreviousLink = false; }

                while (currentIndexCount < View.NumberOfPagesToDisplay && startingNumber <= pageCount)
                {
                    pages.Add(startingNumber);
                    currentIndexCount++;
                    startingNumber++;
                }

                if (startingNumber <= pageCount)
                {
                    View.DisplayNextLink = true;
                    View.NextPageIndex = startingNumber;
                }
                else { View.DisplayNextLink = false; }

                View.DisplayablePages = pages;
                View.DisplayPager = pageCount > 0;
                View.PageCount = pageCount+1;
            }
            else { View.DisplayPager = false; }
        }
    }
}
